*******************************************************************************
* > Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {

*******************************************************************************
* >> Loop over data sets
*******************************************************************************

foreach dataset in gallup gss {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_`dataset', clear

*******************************************************************************
* Set params based on data set
*******************************************************************************

if "`dataset'" == "gallup" {
	rename conf Y
	local mytitle2 "Confidence (Gallup)"
	local treated_target "police"
}
if "`dataset'" == "gss" {
	rename nat Y
	local mytitle2 "Spending (GSS)"
	local treated_target "crimy"
}
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

keep if subgroup == "`subgroup'"

*******************************************************************************
* Variable to save estimates
*******************************************************************************

gen B = .

*******************************************************************************
* >>> Loop over iterations
*******************************************************************************

forvalues k = 1(1)101 {

noisily di "Subgroup `subgroup' and data set `dataset': `k'"

preserve

*******************************************************************************
* Make other series random noise (unless first one)
*******************************************************************************

gen index = _n
save tmp, replace
keep Y
gen shuffle = runiform()
sort shuffle
gen index = _n
rename Y Y_shuffled
merge 1:1 index using tmp
erase tmp.dta

if `k' != 1 replace Y = Y_shuffled if target != "`treated_target'"
	
*******************************************************************************
* Make lags
*******************************************************************************

su year
local mymin = r(min)

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen Y`i'm = mean(Y) if year == `i', by(target)
	egen Y`i' = mean(Y`i'm), by(target)
	drop Y`i'm
	local lags `lags' Y`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "`treated_target'"

*******************************************************************************
* Synth
*******************************************************************************

synth Y `lags', trunit(`r(mean)') trperiod(2014) keep(tmp, replace)

use tmp, clear
erase tmp.dta

*******************************************************************************
* Get RMSPE (one-sided test)
*******************************************************************************

gen D = _Y_treat - _Y_syn
//sc _Y_treat _Y_syn _t

gen DD = D
if "`subgroup'" != "Republican" replace DD = 0 if DD > 0 & _time >= 2014
if "`subgroup'" == "Republican" replace DD = 0 if DD < 0 & _time >= 2014
gen pre_mspem = DD^2 if _time < 2014
egen pre_mspe = mean(pre_mspem)
gen post_mspem = DD^2 if _time >= 2014
egen post_mspe = mean(post_mspem)

gen ratio_mspe = sqrt(post_mspe) / sqrt(pre_mspe)
su ratio_mspe

*******************************************************************************
* Save estimate
*******************************************************************************

restore

replace B = r(mean) if _n == `k'

*******************************************************************************
* >>> Close loop over iterations 
*******************************************************************************

}

*******************************************************************************
* Graph
*******************************************************************************

su B if _n == 1

#delimit;

gr tw 
	hist B if _n != 1, fcol(none) freq
	,
		xline(`r(mean)', lpat(dash)) 
		xlab(0(1)`r(mean)')
		xscale(range(0 `r(mean)'))
		plotregion(style(none))
		ylab(, angle(horiz))
		ytitle("")
		xtitle("")
		title(`mytitle2')
		name(g`dataset', replace)
		;
		
#delimit cr

*******************************************************************************
* >> Close loop over data sets
*******************************************************************************

}

*******************************************************************************
* Combine graphs for subgroup in data set
*******************************************************************************

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine ggallup ggss, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* > Close loop over subgroups
*******************************************************************************

}

*******************************************************************************
* Combine plots for each subgroup
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_sc_placebos_noise.pdf", replace

*******************************************************************************
* End
*******************************************************************************